package de.gwtprime.server.model;

import com.google.web.bindery.requestfactory.shared.Locator;

import de.gwtprime.server.sql.helper.HibernateHelper;

public class EntityLocator extends Locator<EntityBase, Long> {

	@Override
	public EntityBase create(Class<? extends EntityBase> clazz) {
		try {  
			return clazz.newInstance();  
		} catch (InstantiationException e) {  
			throw new RuntimeException(e);  
		} catch (IllegalAccessException e) {  
			throw new RuntimeException(e);  
		}  
	}

	@Override
	public EntityBase find(Class<? extends EntityBase> clazz, Long id) {
		return HibernateHelper.find(clazz, id);
	}

	
	/** 
	   * it's never called ??
	   */ 
	@Override
	public Class<EntityBase> getDomainType() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Long getId(EntityBase domainObject) {
		return domainObject.getId();
	}

	@Override
	public Class<Long> getIdType() {
		return Long.class;
	}

	@Override
	public Object getVersion(EntityBase domainObject) {
		return 0;
	}
	
	public Long persist(EntityBase domainObject) {
		return domainObject.persist();
	}

	public void remove(EntityBase domainObject) {
		domainObject.remove();
	}

}
